import 'package:compass/components/compass.dart';
import 'package:compass/const/direction.dart';
import 'package:compass/const/eight_trigrams.dart';
import 'package:compass/const/sixty_four_hexagrams_of_fu_xi.dart';
import 'package:compass/const/twenty_four_mountain.dart';
import 'package:compass/utils/direction.dart';
import 'package:flutter/material.dart';
import '../components/app_bar.dart';
import '../components/compass_info.dart';
import '../components/permission_switch.dart';
import '../const/type.dart';
import '../style/theme.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key, required this.title});

  final String title;

  @override
  State<HomePage> createState() => _HomePageState();
}

class TextItem extends Text {
  const TextItem.init(String txt, {super.key})
      : super(txt, style: const TextStyle(fontSize: 18));
}

class _HomePageState extends State<HomePage> {
  double _azimuth = 0;
  PositionInfo? _positionInfo;
  bool _hasPermission = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CommonAppBar(title: widget.title),
      body: Container(
        color: themeColorBg,
        child: _hasPermission
            ? Column(
                children: <Widget>[
                  CompassInfo(
                    azimuth: _azimuth,
                    positionInfo: _positionInfo,
                  ),
                  Expanded(
                    child: ClipRect(
                      child: Container(
                        alignment: Alignment.center,
                        padding: const EdgeInsets.all(8),
                        child: CompassBox(
                          onMagneticChanged: ({
                            required double azimuth,
                            required MagneticInfo magnetometerInfo,
                          }) {
                            setState(() {
                              _azimuth = azimuth;
                            });
                          },
                          onPositionChanged: ({
                            required PositionInfo positionInfo,
                          }) {
                            setState(() {
                              _positionInfo = positionInfo;
                            });
                          },
                        ),
                      ),
                    ),
                  ),
                ],
              )
            : PermissionSwitch(
                onChanged: (bool value) {
                  setState(() {
                    _hasPermission = value;
                  });
                },
              ),
      ),
    );
  }
}
